import 'package:fish_redux/fish_redux.dart';

import 'action.dart';
import 'state.dart';


///
/// 刷新数据所需,虽然数据实际是通过 state 完成的,但是 state 刷新数据则需要这里进行数据操作,
/// 所以可以等量代换为这里刷新数据
/// clone,就可以理解为数据备份以后做了改变,这个行为就触发了 setState
///
Reducer<LoginState> buildReducer() {
  return asReducer(
    <Object, Reducer<LoginState>>{
      LoginAction.clearUsername: _clearUsername,
      LoginAction.clearPassword: _clearPasswrod,
      LoginAction.insertUsername: _insertUsername,
    },
  );
}

LoginState _clearUsername(LoginState state, Action action) {
  final LoginState newState = state.clone();
//  newState.usernameController.clear(); //在这里设置username输入框为空
  newState.data['name'] = '';
  return newState;
}

LoginState _clearPasswrod(LoginState state, Action action) {
  final LoginState newState = state.clone();
  newState.passwordController.clear(); //在这里设置清空password输入框为空
  return newState;
}

LoginState _insertUsername(LoginState state, Action action) {
  final LoginState newState = state.clone();
  return newState..data['name'] = action.payload;
  return newState;
}
